//
//  GCZAFNetWorking.m
//  kdxyhd
//
//  Created by Macey Chen on 16/8/11.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import "GCZAFNetWorking.h"

@implementation GCZAFNetWorking


// Post 请求
+ (void)POST:(NSString *)URL params:(NSDictionary * )params success:(void (^)(id response))success
     failure:(void (^)(NSURLSessionDataTask *operation,NSError *error))Error {
    
    // 创建任务管理
    AFHTTPSessionManager *sessionManger = [AFHTTPSessionManager manager];
    [sessionManger POST:URL parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self sucessAction:success task:task responseObject:responseObject];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        Error(task, error);
    }];
    
    
    
}

+ (void)GET:(NSString *)URL success:(void (^)(id response))success
    failure:(void (^)(NSURLSessionDataTask *operation,NSError *error))Error {
    
    AFHTTPSessionManager *sessionManger = [AFHTTPSessionManager manager];
    [sessionManger GET:URL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
       
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self sucessAction:success task:task responseObject:responseObject];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        Error(task, error);
    }];
    
}

+ (void)sucessAction:(void(^)(id responseDic))success task:(NSURLSessionDataTask * _Nonnull)task responseObject:(id  _Nullable)responseObject {
    
    NSDictionary *responseDic;
    if ([responseObject  isKindOfClass:[NSData  class]]) {
        responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
    }else if([responseObject isKindOfClass:[NSString  class]]) {
        NSString *receiveStr = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSData * data = [receiveStr dataUsingEncoding:NSUTF8StringEncoding];
        responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    }
    success(responseObject);
}



@end
